package com.ccn.crm.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    //获取当天13:00时间
    public static Date getDate(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_YEAR, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 13);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        return zero;
    }
    //获取当天13:00时间
    public static Date getToday8(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        return zero;
    }
    //获取当天13:00时间
    public static Date getToday18(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 18);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        return zero;
    }

    /**
     * 获取当前周几
     * @return
     */
    public static int dayForWeek()  {

//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        Date date = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        int dayForWeek = 0;

        if(c.get(Calendar.DAY_OF_WEEK) == 1){

            dayForWeek = 7;

        }else{

            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;

        }

        return dayForWeek;

    }
    /**
     * 获取当天零点时间的时间戳
     */
    public static Long getTodayTimeCuo(){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_YEAR, 0);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        Long time = c.getTime().getTime();
        return  time;
    }

    //根据当前时间获取当前是星期几
    public static int dayForWeekByDate(Date date)  {

//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        int dayForWeek = 0;

        if(c.get(Calendar.DAY_OF_WEEK) == 1){

            dayForWeek = 7;

        }else{

            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;

        }

        return dayForWeek;

    }
    /**

     * 计算两个日期之间相差的天数

     * @param smdate 较小的时间

     * @param bdate 较大的时间

     * @return 相差天数

     * @throws ParseException

     */
    //获取两个时间相差多少天
    public static int daysBetween(Date smdate,Date bdate) {

        Calendar cal = Calendar.getInstance();

        cal.setTime(smdate);

        long time1 = cal.getTimeInMillis();

        cal.setTime(bdate);

        long time2 = cal.getTimeInMillis();

        long between_days=(time2-time1)/(1000*3600*24);
        System.out.println(between_days);
        return Integer.parseInt(String.valueOf(between_days));

    }

    public static Date beforeOneDay() {
        try {
            //        Date date = new Date();//获取当前时间
            Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
//        calendar.add(Calendar.YEAR, -1);//当前时间减去一年，即一年前的时间
//        calendar.add(Calendar.MONTH, -1);//当前时间减去一个月，即一个月前的时间
            calendar.add(Calendar.DAY_OF_MONTH, -1); //当前时间减去一天，即一天前的时间
            calendar.getTimeInMillis();//返回当前时间的毫秒数
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = fmt.format(calendar.getTime());
            Date parse = fmt.parse(date);
            return parse;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date beforeSevenDay() {
        try {
            //        Date date = new Date();//获取当前时间
            Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
//        calendar.add(Calendar.YEAR, -1);//当前时间减去一年，即一年前的时间
//        calendar.add(Calendar.MONTH, -1);//当前时间减去一个月，即一个月前的时间
            calendar.add(Calendar.DAY_OF_MONTH, -8); //当前时间减去一天，即一天前的时间
            calendar.getTimeInMillis();//返回当前时间的毫秒数
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = fmt.format(calendar.getTime());
            Date parse = fmt.parse(date);
            return parse;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date beforeSixDay() {
        try {
            //        Date date = new Date();//获取当前时间
            Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
//        calendar.add(Calendar.YEAR, -1);//当前时间减去一年，即一年前的时间
//        calendar.add(Calendar.MONTH, -1);//当前时间减去一个月，即一个月前的时间
            calendar.add(Calendar.DAY_OF_MONTH, -7); //当前时间减去一天，即一天前的时间
            calendar.getTimeInMillis();//返回当前时间的毫秒数
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = fmt.format(calendar.getTime());
            Date parse = fmt.parse(date);
            return parse;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

        public static Date beforeFifteenDay() {
            try {
                //        Date date = new Date();//获取当前时间
                Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
                calendar.add(Calendar.DAY_OF_MONTH, -15); //当前时间减去一天，即一天前的时间
                calendar.getTimeInMillis();//返回当前时间的毫秒数
                SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String date = fmt.format(calendar.getTime());
                Date parse = fmt.parse(date);
                return parse;

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

    public static Date beforeFourteenDay() {
        try {
            //        Date date = new Date();//获取当前时间
            Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例

            calendar.add(Calendar.DAY_OF_MONTH,-14); //当前时间减去一天，即一天前的时间
            calendar.getTimeInMillis();//返回当前时间的毫秒数
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = fmt.format(calendar.getTime());
            Date parse = fmt.parse(date);
            return parse;
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 日期是否是昨天
     * @param date
     * @return
     */
    public static Boolean formatDateTime(Date date) {
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//        if(time==null ||"".equals(time)){
//            return false;
//        }
//        Date date = null;
//        try {
//            date = format.parse(time);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }

        Calendar current = Calendar.getInstance();

        Calendar today = Calendar.getInstance();	//今天

        today.set(Calendar.YEAR, current.get(Calendar.YEAR));
        today.set(Calendar.MONTH, current.get(Calendar.MONTH));
        today.set(Calendar.DAY_OF_MONTH,current.get(Calendar.DAY_OF_MONTH));
        //  Calendar.HOUR——12小时制的小时数 Calendar.HOUR_OF_DAY——24小时制的小时数
        today.set( Calendar.HOUR_OF_DAY, 0);
        today.set( Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);

        Calendar yesterday = Calendar.getInstance();	//昨天

        yesterday.set(Calendar.YEAR, current.get(Calendar.YEAR));
        yesterday.set(Calendar.MONTH, current.get(Calendar.MONTH));
        yesterday.set(Calendar.DAY_OF_MONTH,current.get(Calendar.DAY_OF_MONTH)-1);
        yesterday.set( Calendar.HOUR_OF_DAY, 0);
        yesterday.set( Calendar.MINUTE, 0);
        yesterday.set(Calendar.SECOND, 0);

        current.setTime(date);

         if(current.before(today) && current.after(yesterday)){
//             System.out.println(true);
            return true;

        }else{
//             System.out.println(false);
            return false;
        }
    }


    /**
     * date2比date1多的天数
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date2,Date date1)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //不同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2-day1) ;
        }
        else    //同一年
        {
//            System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2-day1;
        }
    }
    /**
     * 获取14小时前的时间
     * @return
     */
    public static Date fourteenHoursBefore(Date date){
        long longTime = date.getTime() - 14 * 60 * 60 * 1000;
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return new Date(longTime);
    }

}
